package com.atguigu.pipe;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

/**
 * className: PipeDemo
 * description:
 * date: 2023/3/10 0010-14:18
 * <p>
 * project: nio
 * package: com.atguigu.pipe
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class PipeDemo {

    public static void main(String[] args) throws IOException {
        // 1.获取管道
        Pipe pipe = Pipe.open();

        // 2.获取 sink 通道
        Pipe.SinkChannel sinkChannel = pipe.sink();

        // 3.创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put("atguigu".getBytes());
        byteBuffer.flip();

        // 4.写入数据
        sinkChannel.write(byteBuffer);

        // 5.获取 source 通道
        Pipe.SourceChannel sourceChannel = pipe.source();

        // 6.创建缓冲区，读取数据
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(1024);

        int length = sourceChannel.read(byteBuffer2);

        System.out.println(new String(byteBuffer2.array(), 0, length));

        // 7.关闭通道
        sourceChannel.close();
        sinkChannel.close();
    }
}
